home *** CD-ROM | disk | FTP | other *** search
- Path: news.ichange.com!newsmaster
- From: Jesse Liberty <jl@staff.ichange.com>
- Newsgroups: comp.lang.c++
- Subject: Re: when to inherit, when to compose...
- Date: Wed, 27 Mar 1996 13:41:17 -0500
- Organization: AT&T
- Message-ID: <31598BCD.6BF7@staff.ichange.com>
- References: <4j943p$7es@ulowell.uml.edu> <31587163.1045@datalytics.com>
- NNTP-Posting-Host: 140.244.99.60
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win95; I)
- CC: jl@staff.ichange.com
-
- Rob Stewart wrote:
- >
- > John Peter Lee wrote:
- >> .. Under what circumstances is it more
- > > advantageous to use an inheritance hierarchy for a design, as opposed
- > > to using object containment, and vice versa?
-
- > ...inheritance applies only when the derived class is a form of the
- > base class, you inherit. When it just happens to need to do
- > things another class offers, you use the other class.
-
- Well, yes and no. You actually have a few more options. Let's say you have two classes foo and bar and are trying to decide
- whether bar ought to inherit from foo. The first question is, as you suggest, "is it true that a bar IS A foo?" If the
- answer is yes, then you probably want bar to publicly inherit from foo.
-
- If foo has functionality you want to use, you can implement bar "in terms of" foo by using either private inheritance or
- containment. Private inheritance brings the ability to override methods but if you need more than one "instance" of a foo
- then containment would be a better choice.
-
- Obviously there is more to the decision, and even within the rubric of "containment" there are considerations abuot
- delegation to be made. I suggest a good book on object oriented design such as Martin's book on OO Analysis using C++.
-
-
- -j
-
- ------
- Jesse Liberty [AT&T New Media Services]
- jl@staff.ichange.com ZDNet: 72241,72
- Teach Yourself C++ In 21 Days. Sams 1994
- Teach Yourself MORE C++ In 21 Days. Sams 1996
- Teach Yourself ANSI C++ In 21 Days. Sams 1996
- C++: An Introduction To Programming. Que 1996
-